var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {}
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id
    });
    this.getOrderDetail();
  },
  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.handleOption
        });
        //that.payTimer();
      }
    });
  },
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;

    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },
  comment: function (e) {
    wx.navigateTo({
      //通过 URL  id 传值 传递的值为 设备logicID  和 设备 页面显示的设备 序号
      url: "/pages/commentPost/commentPost?goodsid="+this.data.orderGoods[0].goods_id+"&typeId="+0

    });
  },
  payOrder() {
    let that = this;
    util.request(api.PayPrepayId, {
      orderId: that.data.orderId || 15
    }).then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log(res)
          },
          'fail': function (res) {
            console.log(res)
            
          }
        });
      } else {
        //显示错误提示，并提示用户取消该订单重新生成订单
        wx.showToast({
          image: '/static/images/icon_error.png',
          title: res.err_code_des,
          mask: true
        });
      }
    });

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  //取消订单
  cancelOrder: function (e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '您确认取消这个订单吗？',
      success(res) {
        console.log('that.data.orderId = ', that.data.orderId);
        if (res.confirm) {
          util.request(api.OrderCancel, { orderId: that.data.orderId }, 'POST').then(function (res) {
              console.log('that.data.orderId = ',res.data);
            if (res.errno === 0) {
              if (res.data === 0) {
                // 页面渲染完成
                wx.showToast({
                  title: '取消订单失败！',
                  image: '/static/images/icon_error.png',
                  duration: 2000
                });
              } else {
                // 页面渲染完成
                wx.showToast({
                  title: '取消订单成功！',
                  icon: 'success',
                  duration: 2000
                });
                // wx.navigateTo({
                //   url: '/pages/ucenter/order/order',
                // });
                wx.navigateBack({
                  delta: 1,
                  // success: function () {
                  //   beforePage.onLoad(); // 执行前一个页面的onLoad方法
                  // }
                });
              }
            }

          });
          wx.hideToast();

          console.log("poem_id=" + e.currentTarget.id);
          console.log('用户点击确定');
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },
})